import type { PCBuild, CompatibilityResult } from '../types/components';

// 兼容性检查器类
export class CompatibilityChecker {
  // 检查完整配置的兼容性
  public checkCompatibility(build: PCBuild): CompatibilityResult {
    const incompatibilityReasons: string[] = [];

    // 检查CPU和主板兼容性
    if (build.cpu && build.motherboard) {
      this.checkCpuMotherboardCompatibility(build.cpu, build.motherboard, incompatibilityReasons);
    }

    // 检查内存和主板兼容性
    if (build.ram && build.motherboard) {
      this.checkRamMotherboardCompatibility(build.ram, build.motherboard, incompatibilityReasons);
    }

    // 检查CPU散热器和CPU兼容性
    if (build.cpuCooler && build.cpu) {
      this.checkCpuCoolerCompatibility(build.cpuCooler, build.cpu, incompatibilityReasons);
    }

    // 检查机箱和主板兼容性
    if (build.pcCase && build.motherboard) {
      this.checkCaseMotherboardCompatibility(build.pcCase, build.motherboard, incompatibilityReasons);
    }

    // 检查机箱和CPU散热器兼容性
    if (build.pcCase && build.cpuCooler) {
      this.checkCaseCpuCoolerCompatibility(build.pcCase, build.cpuCooler, incompatibilityReasons);
    }

    // 检查电源功率是否足够
    if (build.powerSupply && (build.cpu || build.gpu)) {
      this.checkPowerSupplyWattage(build, incompatibilityReasons);
    }

    // 检查机箱和GPU兼容性
    if (build.pcCase && build.gpu) {
      this.checkCaseGpuCompatibility(build.pcCase, build.gpu, incompatibilityReasons);
    }

    return {
      isCompatible: incompatibilityReasons.length === 0,
      incompatibilityReasons
    };
  }

  // 检查CPU和主板兼容性
  private checkCpuMotherboardCompatibility(
    cpu: any, 
    motherboard: any, 
    incompatibilityReasons: string[]
  ): void {
    if (cpu.socketType !== motherboard.socketType) {
      incompatibilityReasons.push(`CPU接口 (${cpu.socketType}) 与主板接口 (${motherboard.socketType}) 不兼容`);
    }
  }

  // 检查内存和主板兼容性
  private checkRamMotherboardCompatibility(
    ram: any, 
    motherboard: any, 
    incompatibilityReasons: string[]
  ): void {
    if (ram.type !== motherboard.memoryType) {
      incompatibilityReasons.push(`内存类型 (${ram.type}) 与主板支持的内存类型 (${motherboard.memoryType}) 不兼容`);
    }
  }

  // 检查CPU散热器和CPU兼容性
  private checkCpuCoolerCompatibility(
    cpuCooler: any, 
    cpu: any, 
    incompatibilityReasons: string[]
  ): void {
    if (!cpuCooler.compatibility.includes(cpu.socketType)) {
      incompatibilityReasons.push(`CPU散热器不兼容CPU接口 ${cpu.socketType}`);
    }
  }

  // 检查机箱和主板兼容性
  private checkCaseMotherboardCompatibility(
    pcCase: any, 
    motherboard: any, 
    incompatibilityReasons: string[]
  ): void {
    // 简化的机箱和主板兼容性检查
    // 实际中可能需要更复杂的检查逻辑
    if (motherboard.formFactor === 'ATX' && pcCase.formFactor !== 'ATX') {
      incompatibilityReasons.push(`ATX主板无法安装在 ${pcCase.formFactor} 机箱中`);
    }
  }

  // 检查机箱和CPU散热器兼容性
  private checkCaseCpuCoolerCompatibility(
    pcCase: any, 
    cpuCooler: any, 
    incompatibilityReasons: string[]
  ): void {
    if (cpuCooler.height > pcCase.maxCpuCoolerHeight) {
      incompatibilityReasons.push(`CPU散热器高度 (${cpuCooler.height}mm) 超过机箱最大支持高度 (${pcCase.maxCpuCoolerHeight}mm)`);
    }
  }

  // 检查电源功率是否足够
  private checkPowerSupplyWattage(
    build: PCBuild, 
    incompatibilityReasons: string[]
  ): void {
    if (!build.powerSupply) return;

    // 简化的电源功率计算
    let totalWattage = 100; // 基础功耗
    
    if (build.cpu) {
      totalWattage += build.cpu.tdp;
    }
    
    if (build.gpu) {
      totalWattage += build.gpu.tdp;
    }
    
    // 其他配件功耗估算
    if (build.ram) totalWattage += 10 * (build.ram.modules || 1);
    if (build.storage) totalWattage += 15;
    
    // 预留20%的功率余量
    const requiredWattage = Math.ceil(totalWattage * 1.2);
    
    if (build.powerSupply.wattage < requiredWattage) {
      incompatibilityReasons.push(`电源功率 (${build.powerSupply.wattage}W) 不足，建议至少 ${requiredWattage}W`);
    }
  }

  // 检查机箱和GPU兼容性
  private checkCaseGpuCompatibility(
    pcCase: any, 
    gpu: any, 
    incompatibilityReasons: string[]
  ): void {
    // 这里假设GPU长度信息存在于GPU对象中
    // 实际上我们可能需要根据GPU型号查询其长度
    // 为了简化，这里我们假设一些常见GPU的长度
    let gpuLength = 300; // 默认长度
    
    // 根据GPU型号估算长度
    if (gpu.name.includes('RTX 4090') || gpu.name.includes('RX 7900')) {
      gpuLength = 330;
    } else if (gpu.name.includes('RTX 4080') || gpu.name.includes('RX 7800')) {
      gpuLength = 310;
    }
    
    if (gpuLength > pcCase.maxGpuLength) {
      incompatibilityReasons.push(`GPU长度 (约${gpuLength}mm) 超过机箱最大支持长度 (${pcCase.maxGpuLength}mm)`);
    }
  }
}

// 导出兼容性检查器实例
export const compatibilityChecker = new CompatibilityChecker();